<template>
  <div class="Calendar">
    <div class="calendar-top">
      <div class="left">
        <div>
          <span class="month">
            {{ parseTime(sampleTime, "{m}") }}
          </span>
          月
        </div>
        <div class="year">{{ parseTime(sampleTime, "{y}") }}</div>
      </div>
      <div
        v-if="monthdataDetail && showMonthdataDetail"
        class="monthdataDetail"
      >
        <span> 达标 </span>
        <span>
          <span class="detail-value">
            {{ monthdataDetail.standardDays }}
          </span>
          天
        </span>
        <span>
          <span class="detail-value">
            {{ monthdataDetail.standardProportion }}
          </span>
          <!-- % -->
        </span>
      </div>
    </div>
    <el-calendar :index="month" v-model="sampleTime">
      <template v-slot:dateCell="{ date, data }">
        <p
          :class="{ disabled: data.type != 'current-month' }"
          v-if="data.type != 'current-month'"
          @click.stop
          style="color: #aaa"
        >
          {{ parseTime(date, "{d}") }}
        </p>
        <p
          v-else-if="
            date.getTime() > new Date().getTime() - 24 * 60 * 60 * 1000
          "
          class="disabled"
          @click.stop
        >
          {{ parseTime(date, "{d}") }}
        </p>
        <popover
          v-else
          width="500"
          trigger="click"
          :title="dialogTitle"
          popper-class="dialog-popover"
          placement="bottom-start"
        >
          <div v-if="!isEmpty" class="detail-table" v-loading="dataLoading">
            <el-table :data="tableData" border stripe style="width: 100%">
              <!-- 循环渲染表头和对应的值 -->
              <el-table-column
                v-for="(item, index) in itemList"
                :key="index"
                :label="item.itemName"
                align="center"
              >
                <template slot-scope="scope">
                  <!-- 显示值，如果为空显示占位符 -->
                  {{ scope.row.effectiveValue || "-" }}
                </template>
              </el-table-column>
            </el-table>
            <!-- <div class="item-value">
              <div class="item-top" v-show="IsDrink == '0'">
                <img src="@/assets/images/i_szzs.png" alt="" />
                水质指数：{{ detail.Index || "--" }}
              </div>
              <div
                style="
                  width: 1px;
                  height: 28px;
                  border: 1px solid rgb(48, 126, 255, 0.3);
                "
              ></div>
              <div class="item-top">
                <img src="@/assets/images/i_pj.png" alt="" />
                水质类别：
                <span :class="'qualityLevel' + detail.QualityLevel">
                  {{ detail.QualityLevelName || "--" }}
                </span>
              </div>
            </div>
            <div class="box-list">
              <div
                v-for="(item, index) in itemList"
                :key="index"
                class="item-box"
              >
                <p class="box-name">
                  {{ item.CNName }}
                </p>
                <p
                  class="box-value"
                  :class="'level' + detail[item.ID + '_Quality']"
                  :style="{
                    color: getColorbylevel(
                      detail[item.ID + '_Quality']
                        ? detail[item.ID + '_Quality']
                        : '无类别'
                    ),
                  }"
                >
                  {{ detail[item.ID] || "--" }}
                </p>
              </div>
            </div> -->
          </div>
          <!-- 空状态插槽 -->
          <div v-else>
            <el-empty :image-size="120" description="暂无监测数据"> </el-empty>
          </div>
          <p
            slot="reference"
            class="border"
            :class="getClass(date)"
            @click="habdleDetail(date)"
          >
            {{ parseTime(date, "{d}") }}
          </p>
        </popover>
      </template>
    </el-calendar>
  </div>
</template>
<script>
import { getColorbylevel } from "@/utils/levelColor";
import { getAutoWaterCalendarDetail } from "@/api/waterBusiness/interface.js";
// import { getDayDetailData } from "@/api/base/waterQuality";
export default {
  name: "Calendar",
  props: {
    month: {
      type: [String, Number],
      default() {
        return new Date().getMonth() + 1;
      },
    },
    year: {
      default() {
        return new Date();
      },
    },
    response: {
      type: Array,
      default: () => {
        return [];
      },
    },
    monthdata: {
      type: Array,
      default: () => {
        return [];
      },
    },
    IsDrink: {
      type: [String, Number],
      default: "0",
    },
    dmid: {
      type: String,
      default: "",
    },
  },

  computed: {
    showMonthdataDetail() {
      const now = new Date().getTime();
      const time = new Date(this.sampleTime).getTime();
      return now > time;
    },
    propsYear() {
      return this.parseTime(new Date(this.year), "{y}");
    },
    monthdataDetail() {
      return this.monthdata.find((item) => {
        const month = this.parseTime(new Date(item.month), "{m}");
        return month == this.month;
      });
    },
    sampleTime: {
      get() {
        return this.monthdataDetail?.month ?? "";
      },
      set() {},
    },
    // 转换数据格式，将所有指标值整合为一行数据
    tableData() {
      const rowData = {};
      this.itemList.forEach((item) => {
        // 以itemName为键，effectiveValue为值构建一行数据
        rowData[item.itemName] = item.effectiveValue || "-";
      });
      // 返回包含一行数据的数组
      return [rowData];
    },
  },

  data() {
    return {
      getColorbylevel,
      value: null,
      dialogTitle: "",
      detail: {},
      itemList: [],
      dataLoading: false,
      isEmpty: false,
    };
  },
  methods: {
    getClass(date) {
      if (date.getTime() > new Date().getTime() - 24 * 60 * 60 * 1000) return;
      const thisTime = this.parseTime(new Date(date), "{y}-{m}-{d}");
      if (this.response.length == 0) return;
      // console.log(">>>this.response", this.response);

      const level =
        this.response.find((item) => item.day == thisTime)?.["qualityLevel"] ??
        "";
      if (level) {
        // return "qualityLevel" + level;
        return this.qualityLevelCode(level);
      } else {
        return;
      }
    },
    qualityLevelCode(code) {
      const levelMap = {
        0: "qualityLevel",
        1: "qualityLevel1",
        2: "qualityLevel2",
        3: "qualityLevel3",
        4: "qualityLevel4",
        5: "qualityLevel5",
        6: "qualityLevel6",
        无类别: "qualityLevel",
        未测: "qualityLevel",
        无数据: "qualityLevel",
        Ⅰ类: "qualityLevel1",
        Ⅱ类: "qualityLevel2",
        Ⅲ类: "qualityLevel3",
        Ⅳ类: "qualityLevel4",
        Ⅴ类: "qualityLevel5",
        劣Ⅴ类: "qualityLevel6",
        "": "qualityLevel",
      };
      return levelMap[code] || "";
    },
    disablClass(date) {
      if (
        new Date(date).getTime() >
        new Date().getTime() - 24 * 60 * 60 * 1000
      ) {
        return "disabled";
      }
    },
    habdleDetail(date) {
      debugger;
      if (date.getTime() > new Date().getTime() - 24 * 60 * 60 * 1000) return;
      const thisTime = this.parseTime(new Date(date), "{y}-{m}-{d}");
      this.isEmpty = false;
      this.dialogTitle = "";
      this.detail = {};
      this.dataLoading = true;
      let query = {
        id: this.dmid,
        day: thisTime,
      };

      getAutoWaterCalendarDetail(query)
        .then((result) => {
          console.log(">>>result", result);
          this.dataLoading = false;
          this.itemList = result.data.dataList || [];
        })
        .catch((error) => {
          this.dialogTitle = "";
          this.detail = {};
          this.dataLoading = false;
          this.itemList = [];
        });
    },
  },
};
</script>
<style scoped lang="scss">
@import "~@/style/utils.scss";
.theme-dark {
  .box-list {
    .item-box {
      border: 1px solid #307eff;
    }
    .box-name {
      color: #ffffff;
      background: rgb(48, 126, 255, 0.3);
    }
  }
}

::v-deep {
  .el-calendar__button-group,
  .el-calendar__header {
    display: none;
  }
  .el-calendar__body {
    padding: 8px 20px;
    position: relative;
  }
  .el-calendar-table {
    td {
      border: none !important;
    }
    .is-selected {
      background-color: transparent;
    }
    .el-calendar-day {
      height: 40px;
      padding: 0;
      p {
        margin: 0;
        padding: 0;
        width: 30px;
        height: 30px;
        min-width: 28px;
        line-height: 28px;
        text-align: center;
        border-radius: 50%;
        font-size: 16px;
        font-family: Barlow Condensed SemiBold;
        background-clip: content-box;
      }
      .disabled {
        cursor: not-allowed;
      }
    }
  }
  .el-calendar {
    position: relative;
    &::before {
      content: attr(index);
      position: absolute;
      top: calc(50% - 80px);
      left: calc(50% - 40px);
      font-family: Barlow Condensed SemiBold;
      font-weight: 400;
      font-size: 120px;
      color: #333333;
      font-style: italic;
      opacity: 0.1;
    }
  }
}
.Calendar {
  position: relative;
  border: 1px solid;
  @include theme {
    border-color: getTheme("borderColor") !important;
  }
  .calendar-top {
    padding: 4px 12px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    font-family: Barlow Condensed SemiBold;
    @include theme {
      border-bottom: 1px solid getTheme("borderColor");
    }
    .left {
      font-weight: 700;
      @include theme {
        color: getTheme("textColor");
      }
      .month {
        font-size: 24px;
      }
      .year {
        font-size: 18px;
      }
    }
  }
  .monthdataDetail {
    height: 28px;
    line-height: 28px;
    // background-color: rgba($color: #409eff, $alpha: 0.1);
    // border-radius: 12px 0;
    display: flex;
    align-items: center;
    justify-content: space-between;
    font-family: Barlow Condensed SemiBold;
    font-size: 14px;
    @include theme {
      color: getTheme("textColor");
    }
    > span {
      height: 100%;
      padding: 0 4px;
      white-space: nowrap;
      &:first-child {
        padding: 0 8px;
        /* background-color: var(--current-color); */
        // background-color: #409eff;
        color: #307eff;
        font-weight: bold;
        font-size: 16px;
        // border-radius: 12px 0;
      }
      .detail-value {
        font-style: italic;
        font-size: 22px;
      }
    }
  }
}
.detail-table {
  width: 100%;
  text-align: center;
  background-color: transparent;
  .item-value {
    font-size: 16px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    @include theme {
      color: getTheme("textColor");
    }
    .item-top {
      display: flex;
      align-items: center;
      margin: 5px 0;
    }
  }
  .item-name {
    font-size: 12px;
    @include theme {
      color: getTheme("textColor");
    }
  }
}
.box-list {
  display: grid;
  grid-template-columns: 1fr 1fr 1fr 1fr;
  gap: 10px 32px;
  .item-box {
    border: 1px solid #e6edfc;
    padding: 3px;
    // @include theme {
    // border: 1px solid #307eff;
    // }
  }
  .box-name {
    color: #333333;
    background: #e6edfc;
    font-family: Source Han Sans CN;
    font-weight: 500;
    font-size: 14px;
    text-align: center;
    line-height: 28px;
    // @include theme {
    // color: getTheme("textColor");
    // background: rgb(48, 126, 255, 0.3);
    // }
  }
  .box-value {
    font-family: Barlow Condensed SemiBold;
    font-weight: 400;
    text-align: center;
    font-size: 18px;
    line-height: 28px;
  }
  .level1,
  .level4 {
    text-shadow: 1px 1px 1px #666;
  }
}
.calendar-box {
  display: grid;
  grid-template-columns: repeat(6, 1fr);
  grid-gap: 12px;
  height: 100%;
  overflow: auto;
}
</style>
